package hikari.persistence.handlersimpl;

import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.assets.AssetsTree;
import hikari.objects3d.Basic3dObject;
import hikari.objects3d.Custom3DObject;
import hikari.persistence.file.FileSection;
import hikari.persistence.persisthandlers.IPersistantObject3dReadHandler;
import hikari.persistence.persisthandlers.PersistantHandler;
import java.io.IOException;
import org.swig.jirr.ISceneManager;

@WiringComponent(singleton = true, lazy = true)
@PersistantHandler(sectionName = "cModel")
public class CustomModelLoadHandler implements IPersistantObject3dReadHandler {

    @Wire
    AssetsTree assetsTree;

    private CustomModelLoadHandler() {
    }

    @Override
    public Basic3dObject read(ISceneManager sceneManager, Basic3dObject object, FileSection section) throws IOException {
        String relativePath = section.readString(section.getChunk("path"));
        Custom3DObject object3d = Custom3DObject.loadModelByPath(assetsTree.getAbsoluteAssetPathFromRelativePath(relativePath), sceneManager);
        if (object3d != null) {
            object3d.setRelativeModelPath(relativePath);
        }
        return object3d;
    }
}
